﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNADota.UI;
using Microsoft.Xna.Framework;
using XNADota.Input;
using XNADota.Camera;
using XNADota.Graphics;
using XNADota.MDX;
using Microsoft.Xna.Framework.Graphics;
using XNADota.GamePlay.Ability;

namespace XNADota.GamePlay
{
    public class PlayerCommander : IInputHandler
    {
        private CommanderScreen _CommanderScreen;

        public const int MaxAbilityCount = 12;
        private AbilityObject[] Abilities = new AbilityObject[MaxAbilityCount];

        public PlayerCommander(Game game)
        {
            XNADota.Instance.InputManager.Add(this);
            _CommanderScreen = new CommanderScreen(game);
        }

        public void Update(GameTime gameTime)
        {
            // display objects
            PaletteInstance[] selectedObjects = SceneManager.Instance.SelectedObjects();

            if (selectedObjects.Length == 1)
            {
                _CommanderScreen.ControlPanel.Clear();

                // check the object is merchant
                UnitObject unit = selectedObjects[0] as UnitObject;
                if (unit.Abilities.ContainsKey("Apit") == true)
                {
                    unit.Abilities["Apit"].MerchantAbility.ApplyControlPanel(_CommanderScreen.ControlPanel);
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            _CommanderScreen.Draw(gameTime);
        }

        public void ProcessInput(GameTime gameTime, InputState inputState)
        {
            var CameraManager = XNADota.Instance.CameraManager;

            if (inputState.LeftButtonSingleClick)
            {
                Microsoft.Xna.Framework.Ray ray = XNADota.Instance.CameraManager.ActiveCamera.GetRayFromScreen((int)inputState.CursorPostion.X, (int)inputState.CursorPostion.Y);

                SceneManager.Instance.DeselectAll();
                PaletteInstance[] selectObjects = SceneManager.Instance.Select(ray);

                for (int i = 0; i < selectObjects.Length; i++)
                {
                    if (selectObjects[i] == null)
                        break;
                    selectObjects[i].SetSelected(true);
                }
            }

            Rectangle selectRect = inputState.SelectRectangle;
            if (selectRect.Width > 0 && selectRect.Height > 0)
            {
                if (inputState.LeftButtonAlwaysPressed)
                {
                    PaletteRendererManager manager = SceneManager.Instance.PaletteRendererManager;
                    PaletteRenderer renderer = manager.GetPaletteRenderer(EPaletteRendererType.Sprite, "AlphaBlend");
                    Texture2D tex = ReplaceableTextures.Red;
                    int bold = 1;
                    SceneManager.Instance.AddIntoRenderQueue(
                        new SpriteInstance(selectRect.X, selectRect.Y, bold, selectRect.Height, 0, 0, 10, 10, tex, renderer));
                    SceneManager.Instance.AddIntoRenderQueue(
                        new SpriteInstance(selectRect.X + selectRect.Width, selectRect.Y, bold, selectRect.Height, 0, 0, 10, 10, tex, renderer));
                    SceneManager.Instance.AddIntoRenderQueue(
                        new SpriteInstance(selectRect.X, selectRect.Y, selectRect.Width, bold, 0, 0, 10, 10, tex, renderer));
                    SceneManager.Instance.AddIntoRenderQueue(
                        new SpriteInstance(selectRect.X, selectRect.Y + selectRect.Height, selectRect.Width, bold, 0, 0, 10, 10, tex, renderer));
                }
                if (inputState.LeftButtonJustRelease)
                {
                    PaletteInstance[] selectObjects = SceneManager.Instance.Select(inputState.SelectRectangle);

                    for (int i = 0; i < selectObjects.Length; i++)
                    {
                        if (selectObjects[i] == null)
                            break;
                        selectObjects[i].SetSelected(true);
                    }
                }
            }
        }
    }
}
